﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports;

namespace BSC_OTC_Management.Forms
{
    public partial class FrmPreviewReport : Form
    {
        string _path = "";
        string filename = "";

        public string V_RptFile_Goc = "";
        public string V_RptFile_Tmp = "";

        DataTable TBL_RPT = new DataTable("Reports");
        string RPTID = "";

        ReportDocument rptdoc;

        public FrmPreviewReport(DataTable table, String rptid)
        {
            InitializeComponent();
            _path = Code.StaticVariable.v_StartUP_Path + "\\Reports\\"; 
            if (!System.IO.Directory.Exists(_path))
                System.IO.Directory.CreateDirectory(_path);

            if (!System.IO.Directory.Exists(_path + "temps\\"))
                System.IO.Directory.CreateDirectory(_path + "temps\\");

            filename = rptid;

            V_RptFile_Goc = _path + filename + ".rpt";
            V_RptFile_Tmp = _path + "temps\\" + filename;

            TBL_RPT = table;
            RPTID = rptid;

            //load report
            Load_RP(TBL_RPT);
        }

        //create new report
        public void Create_RP()
        {
            try
            {
                SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                saveFileDialog1.InitialDirectory = _path + "temps\\";
                saveFileDialog1.Title = "Save as";
                saveFileDialog1.Filter = "RPT files (*.rpt)|*.rpt|PDF files(.pdf)|*.pdf|All files (*.*)|*.*";
                saveFileDialog1.FilterIndex = 3;
                saveFileDialog1.RestoreDirectory = true;
                saveFileDialog1.FileName = filename;
                //if (File.Exists(saveFileDialog1.FileName))
                //    saveFileDialog1.CheckFileExists = true;
                //saveFileDialog1.CheckPathExists = true;
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    switch (saveFileDialog1.FilterIndex)
                    {
                        case 1:
                            //export to .rpt
                            rptdoc.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.CrystalReport, saveFileDialog1.FileName);
                            break;
                        case 2:
                            //export to pdf
                            ExportOptions CrExportOptions;
                            DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
                            PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();
                            CrDiskFileDestinationOptions.DiskFileName = saveFileDialog1.FileName;
                            CrExportOptions = rptdoc.ExportOptions;
                            {
                                CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
                                CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
                                CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
                                CrExportOptions.FormatOptions = CrFormatTypeOptions;
                            }
                            rptdoc.Export();
                            break;
                        default:
                            //export to .rpt
                            rptdoc.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.CrystalReport, saveFileDialog1.FileName);
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        //load sample report
        public Boolean Load_RP(DataTable v_TblReportSource)
        {
            try
            {
                //-----------1. Load file RPT goc len 1 temp rptdoc ---------------------------------------------------------------------------------------
                rptdoc = new ReportDocument();
                
                rptdoc.Load(V_RptFile_Goc, CrystalDecisions.Shared.OpenReportMethod.OpenReportByTempCopy);

                //-----------2. Truyen tham so cho file temp RPT ------------------------------------------------------------------------------------------
                //rptdoc.SetParameterValue("@A1", "hehe")
                DataSet ds_rePort = new DataSet();
                ds_rePort.Tables.Add(v_TblReportSource);
                //DatNT edit ds_rePort.Tables[0]
                DataTable tbl_test = ds_rePort.Tables[0];
                rptdoc.SetDataSource(tbl_test);

                //-----------3. Save as xuong thu muc temp       ------------------------------------------------------------------------------------------
                string d = DateTime.Now.ToString("ddMMyyhhmmss");
                if (!File.Exists(V_RptFile_Tmp + ".rpt"))
                    rptdoc.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.CrystalReport, V_RptFile_Tmp + ".rpt");
                else
                    rptdoc.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.CrystalReport, V_RptFile_Tmp + "_" + d + ".rpt");

                //-----------4.hiển thị Report Viewer
                crystalReportViewer1.ReportSource = rptdoc;
                crystalReportViewer1.Refresh();
                return true;

            }
            catch (Exception ex)
            {
                ex.ToString();
                return false;
            }


        }

        private void btt_download_Click(object sender, EventArgs e)
        {
            try
            {
                crystalReportViewer1.ExportReport();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            //Save file neu ko su dung Export Report
            //Create_RP();
        }

        private void btt_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btt_print_Click(object sender, EventArgs e)
        {
            try
            {
                crystalReportViewer1.PrintReport();
                /* code cho may in neu ko co PrintReport
                 * PrintDialog printDialog = new PrintDialog();
                if (printDialog.ShowDialog() == DialogResult.OK)
                {
                    System.Drawing.Printing.PrintDocument printDocument1 = new System.Drawing.Printing.PrintDocument();
                    //Get the Copy times
                    int nCopy = printDocument1.PrinterSettings.Copies;
                    //Get the number of Start Page
                    int sPage = printDocument1.PrinterSettings.FromPage;
                    //Get the number of End Page
                    int ePage = printDocument1.PrinterSettings.ToPage;
                    rptdoc.PrintOptions.PrinterName = printDocument1.PrinterSettings.PrinterName;
                    //Start the printing process.  Provide details of the print job
                    rptdoc.PrintToPrinter(nCopy, false, sPage, ePage);
                }*/
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }


    }
}
